//
//  JSLLoanDetialController.m
//  JingleSpeedLoan
//
//  Created by 宋质彬 on 2018/7/20.
//  Copyright © 2018年 宋质彬. All rights reserved.
//

#import "JSLLoanDetialController.h"
#import "JSLLoanDetialScrollView.h"
#import "H5WebViewController.h"
@interface JSLLoanDetialController ()<JSLLoanDetialScrollViewDelegate>
@property (nonatomic , weak) JSLLoanDetialScrollView *loanDetialScroller;
@property (nonatomic , strong) NSDictionary *info;

@property (nonatomic , strong) NSDictionary *detialInfo;
@property (nonatomic , copy) NSString *productId;
@end

@implementation JSLLoanDetialController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self baseConfig:PageGobackTypePop];
    
    [self setUpView];
}


- (void)getLoanDetialInfo {
    NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init];
    parameters[@"productId"] = self.productId;
    parameters[@"publishApp"] = @"2";

    [HttpRequest POST:LOAN_DERIAL_PRODUCT_URL parameters:parameters success:^(id  _Nullable result, NSString * _Nullable successMessage) {
        NSLog(@"%@",result);
        
        self.detialInfo = result[@"data"][@"product"];
        
        [self.loanDetialScroller configLoanDetial:self.detialInfo];
        // 跳转
        NSLog(@"%@",result);
        
        
        
    } failure:^(NSInteger code, NSString * _Nullable errorMessage) {
        
        [self showMessage:errorMessage];
    }];
}

-(void)clickNowApplyButtion:(NSMutableDictionary *)dict {

    if ([UserManager checkLogin:self]) {
        dict[@"userPhone"] = [UserManager manager].userInfo.userPhone;
        [HttpRequest POST:LOAN_APPLY_SAVEORDER parameters:dict success:^(id  _Nullable result, NSString * _Nullable successMessage) {
            NSLog(@"%@",result);
            H5WebViewController *webVC = [[H5WebViewController alloc] init];
            webVC.url = self.detialInfo[@"productUrl"];
            [self pushViewController:webVC animated:YES];
            
            
        [MobClick event:@"invest_order" attributes:@{@"product_name":self.detialInfo[@"productName"]}];
            
        } failure:^(NSInteger code, NSString * _Nullable errorMessage) {
            [self showMessage:errorMessage];
            
        }];
    }
    
}

- (void)configLoanDetial:(NSDictionary *)info fromFlag:(LoanDetialType)fromFlag {
//    {
//        actionType = 1;
//        bannerId = 10;
//        bannerImage = "http://www.jieduoqian.com/image/94245593-2b9b-49ca-a30c-028b7549142a.jpg";
//        bannerTitle = "\U767e\U5ea6\U6709\U94b1\U82b1";
//        targetUrl = "antt://att?productId=21";
//    }
    
    if (fromFlag == LoanFromNormal) {
        self.productId = info[@"productId"];
        self.navigationItem.title = info[@"productName"];
    }else {
        
        
        NSArray *array = [info[@"targetUrl"] componentsSeparatedByString:@"="];
        self.productId = [array lastObject];
        self.navigationItem.title = info[@"bannerTitle"];
    }
    
    
    
    [self getLoanDetialInfo];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setUpView {
    JSLLoanDetialScrollView *scrollerView = [[JSLLoanDetialScrollView alloc] initWithFrame:CGRectZero];
    
    
  
    scrollerView.loanDetialScrollViewDelegat = self;
    scrollerView.pagingEnabled = NO;
    scrollerView.bounces = YES;
    scrollerView.scrollEnabled = YES;
    [self.view addSubview:scrollerView];
    scrollerView.contentOffset = CGPointMake(0, 0);  // 默认显示x=0，y=0的地方
    scrollerView.contentSize = CGSizeMake( SCREEN_WIDTH, 811*AutoSizeScaleXX +32*AutoSizeScaleXX +50*AutoSizeScaleXX);
    self.loanDetialScroller = scrollerView;
    
    self.loanDetialScroller.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-49);
    
}
@end
